import 'package:boss/common/common.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/provider/package_detail_provider.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/form/title_switch.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import 'package_panel.dart';

class BasePanel extends StatefulWidget {
  @override
  _BasePanelState createState() => _BasePanelState();
}

class _BasePanelState extends State<BasePanel> {
  @override
  Widget build(BuildContext context) {
    return Consumer<PackageDetailProvider>(
      builder: (context, packageDetailProvider, _) {
        var package = packageDetailProvider.package;

        var selectedMenu = packageDetailProvider.menus.firstWhere(
          (item) => item.menuId == package.menuId,
          orElse: () => null,
        );
        return PackagePanel(
          title: '基本信息',
          children: <Widget>[
            BaseTextFormField(
              prefixText: '套餐名称',
              isRequired: true,
              hintText: '例如：儿童套餐、亲子套餐',
              initialValue: package.title,
              onChanged: (value) {
                packageDetailProvider.setPackageName(value);
              },
            ),
            SelectFormField<StoreMenu>(
              title: '套餐分类',
              hintText: '请选择',
              isRequired: true,
              items: packageDetailProvider.menus
                  .map((item) => SelectItem(
                        title: item.menuName,
                        value: item,
                      ))
                  .toList(),
              value: selectedMenu,
              onChanged: (value) {
                packageDetailProvider.setPackageMenu(value);
              },
            ),
            BaseTextFormField(
              prefixText: '售卖价格',
              isRequired: true,
              hintText: '请输入',
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
              initialValue: AmountUtil.price2String(package.price),
              onChanged: (value) {
                packageDetailProvider.setPackagePrice(AmountUtil.string2Price(value));
              },
            ),
            if (Constant.commodityType == CommodityType.tang)
              TitleSwitch(
                title: '扫码点餐',
                value: package.isH5,
                onChanged: (flag) {
                  packageDetailProvider.setH5(flag);
                },
              ),
          ],
        );
      },
    );
  }
}
